package mybatis.plus.injection;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;
import mybatis.plus.generator.CodeGenerator;

import java.sql.SQLException;
import java.util.HashMap;

/**
 * 代码生成
 */
public class ERCAdminInjectionGenerator {

    /**
     * 执行 run
     */
    public static void main(String[] args) throws SQLException {
        String pkgPath = "/Users/cool/starProjects/erc-admin";
        FastAutoGenerator.create(CodeGenerator.ERC_URL, CodeGenerator.ERC_URL_USERNAME, CodeGenerator.ERC_URL_PASSWORD)
                // 全局配置
                .globalConfig(builder -> builder.outputDir(pkgPath).author("ypf")
                        .disableOpenDir().fileOverride().enableSpringdoc())
                // 包配置
                .packageConfig(builder -> builder.parent("com.starcharge.overseas.erc.admin")
                        .controller("service.controller").service("service.service").serviceImpl("service.service.impl").mapper("dao").xml("mapper").entity("dao.module")
                        .pathInfo(new HashMap<OutputFile, String>() {
                            {
                                put(OutputFile.xml, "/Users/cool/starProjects/erc-admin/erc-admin-dao/src/main/resources/mapper");
                                put(OutputFile.entity, "/Users/cool/starProjects/erc-admin/erc-admin-dao/src/main/java/com/starcharge/overseas/erc/admin/dao/module");
                                put(OutputFile.service, "/Users/cool/starProjects/erc-admin/erc-admin-service/src/main/java/com/starcharge/overseas/erc/admin/service/service");
                                put(OutputFile.serviceImpl, "/Users/cool/starProjects/erc-admin/erc-admin-service/src/main/java/com/starcharge/overseas/erc/admin/service/service/impl");
                                put(OutputFile.mapper, "/Users/cool/starProjects/erc-admin/erc-admin-dao/src/main/java/com/starcharge/overseas/erc/admin/dao");
                                put(OutputFile.controller, "/Users/cool/starProjects/erc-admin/erc-admin-service/src/main/java/com/starcharge/overseas/erc/admin/service/controller");
                            }
                        }))

                // 模板配置
//                .templateConfig(builder -> builder.disable(TemplateType.XML, TemplateType.CONTROLLER))
//                 注入配置
                .injectionConfig(builder -> builder.beforeOutputFile((tableInfo, objectMap) -> {
//                            String entity = (String)objectMap.get("entity");
//                            String substring = entity.substring(1);
//                            objectMap.put("entity", substring);
//                            tableInfo.setEntityName(substring);
                            // 这里是预处理输出模板信息
                            System.out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap);
                        })
                        // 这里是自定义传入模板参数值
//                        .customMap(Collections.singletonMap("test", "baomidou"))
//                        .customFile(new HashMap<String, String>() {{
//                            // 这里可以自定义自己的模板
//                            put("hi.txt", "/templates/hi.vm");
//                            put("sample.vue", "/templates/vue.vm");
//                        }})
                        .build())
                // 策略配置 sample
                .strategyConfig((scanner, builder) -> builder.addInclude(scanner.apply("请输入表名？"))
                        .entityBuilder().convertFileName(
                                new ConverterFileName() {
                                    @Override
                                    public String convert(String entityName) {
                                        return entityName.substring(1);
                                    }
                                }

                        ).

                        enableLombok().
                        controllerBuilder().convertFileName(
                                new ConverterFileName() {
                                    @Override
                                    public String convert(String entityName) {
                                        return entityName.substring(1) + "Controller";
                                    }
                                }

                        ).
                        enableRestStyle().serviceBuilder().
                        convertServiceFileName(new ConverterFileName() {
                            @Override
                            public String convert(String entityName) {
                                return entityName.substring(1) + "Service";
                            }
                        }).
                        convertServiceImplFileName(new ConverterFileName() {
                            @Override
                            public String convert(String entityName) {
                                return entityName.substring(1) + "ServiceImpl";
                            }
                        }).mapperBuilder().
                        convertMapperFileName(
                                new ConverterFileName() {
                                    @Override
                                    public String convert(String entityName) {
                                        return entityName.substring(1) + "Mapper";
                                    }
                                }).
                        convertXmlFileName(
                                new ConverterFileName() {
                                    @Override
                                    public String convert(String entityName) {
                                        return entityName.substring(1) + "Mapper";
                                    }
                                }
                        )


                )
                // 模板引擎的某个方法
//                .templateEngine(new VelocityTemplateEngine() {
//                    @Override
//                    protected void outputCustomFile(Map<String, String> customFile, TableInfo tableInfo, Map<String, Object> objectMap) {
//                        String otherPath = getPathInfo(OutputFile.other);
//                        customFile.forEach((key, value) -> {
//                            String fileName = String.format((otherPath + File.separator + "%s"), key);
//                            outputFile(new File(fileName), objectMap, value);
//                        });                    }
//                })
                // 执行
                .execute();
    }
}
